package com.medvalley.tcm.service.impl;

import com.medvalley.tcm.base.BusinessException;
import com.medvalley.tcm.base.EmptyException;
import com.medvalley.tcm.dao.*;
import com.medvalley.tcm.domain.*;
import com.medvalley.tcm.service.YangShengService;
import com.medvalley.tcm.vo.inputvo.SportsInputVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

@Service
public class YangShengServiceImpl implements YangShengService {

    @Autowired
    private YangShengAcupunctureDao yangShengAcupunctureDao;

    @Autowired
    private YangShengEatDao yangShengEatDao;

    @Autowired
    private YangShengSeaonDao yangShengSeaonDao;

    @Autowired
    private YangShengSportDao yangShengSportDao;

    @Autowired
    private YangShengTeaDao yangShengTeaDao;

    @Autowired
    private YangShengMethodDao yangShengMethodDao;
    @Autowired
    private YangShengXieZiDao yangShengXieZiDao;

    private static int STATUS_1 = 1;

    private static int STATUS_0 = 0;

    /**
     * 艾灸
     **/
    @Override
    public void saveYangShengAcupuncture(YangShengAcupuncture yangShengAcupuncture) {
        yangShengAcupunctureDao.save(yangShengAcupuncture);
    }

    @Override
    public YangShengAcupuncture getYangShengAcupunctureById(long id) {
        YangShengAcupuncture yangShengAcupuncture = yangShengAcupunctureDao.findById(id).orElse(null);
        if (yangShengAcupuncture == null) {
            throw new EmptyException(" 没有相关数据 ");
        }
        return yangShengAcupuncture;
    }

    @Override
    public YangShengAcupuncture getYangShengAcupunctureByName(String name) {
        YangShengAcupuncture yangShengAcupuncture = yangShengAcupunctureDao.findOneByMoxibustionNameAndStatus(name, STATUS_1);
        return yangShengAcupuncture;
    }

    @Override
    public YangShengAcupuncture updateYangShengAcupuncture(YangShengAcupuncture yangShengAcupuncture) {
        yangShengAcupunctureDao.save(yangShengAcupuncture);
        return yangShengAcupuncture;
    }

    @Override
    public Page<YangShengAcupuncture> pageYangShengAcupuncture(int pageNo, int pageSize) {
        pageNo = pageNo - 1 <= 0 ? 0 : pageNo - 1;
        Pageable pageable = PageRequest.of(pageNo, pageSize, Sort.Direction.ASC, "id");
        Page<YangShengAcupuncture> results = yangShengAcupunctureDao.findAllByStatus(pageable, STATUS_1);
        return results;
    }

    @Override
    public void deleteYangShengAcupuncture(long id) {
        YangShengAcupuncture yangShengAcupuncture = yangShengAcupunctureDao.findById(id).get();
        yangShengAcupuncture.setStatus(STATUS_0);
        yangShengAcupunctureDao.saveAndFlush(yangShengAcupuncture);
    }

    /**
     * 茶饮
     **/
    @Override
    public void saveYangeShengTea(YangShengTea yangShengTea) {
        yangShengTeaDao.save(yangShengTea);
    }

    @Override
    public YangShengTea getYangShengTeaById(long id) {
        YangShengTea yangShengTea = yangShengTeaDao.findById(id).orElse(null);
        if (yangShengTea == null) {
            throw new EmptyException(" 没有相关数据 ");
        }
        return yangShengTea;
    }

    @Override
    public YangShengTea getYangShengTeaByName(String name) {
        YangShengTea yangShengTea = yangShengTeaDao.findOneByTeaNameAndStatus(name, STATUS_1);
        return yangShengTea;
    }

    @Override
    public YangShengTea updateYangShengTea(YangShengTea yangShengTea) {
        yangShengTeaDao.save(yangShengTea);
        return yangShengTea;
    }

    @Override
    public Page<YangShengTea> pageYangShengTea(int pageNo, int pageSize) {
        pageNo = pageNo - 1 <= 0 ? 0 : pageNo - 1;
        Pageable pageable = PageRequest.of(pageNo, pageSize, Sort.Direction.ASC, "id");
        Page<YangShengTea> results = yangShengTeaDao.findAllByStatus(pageable, STATUS_1);
        return results;
    }

    @Override
    public void deleteYangShengTea(long id) {
        YangShengTea yangShengTea = yangShengTeaDao.findById(id).get();
        yangShengTea.setStatus(STATUS_0);
        yangShengTeaDao.saveAndFlush(yangShengTea);
    }

    /**
     * 膳食
     */
    @Override
    public void saveYangeShengEat(YangShengEat yangShengEat) {
        yangShengEatDao.save(yangShengEat);
    }

    @Override
    public YangShengEat getYangShengEatById(long id) {
        YangShengEat yangShengEat = yangShengEatDao.findById(id).orElse(null);
        if (yangShengEat == null) {
            throw new EmptyException(" 没有相关数据 ");
        }
        return yangShengEat;
    }

    @Override
    public YangShengEat getYangShengEatByName(String name) {
        YangShengEat yangShengEat = yangShengEatDao.findOneByDietNameAndStatus(name, STATUS_1);
        return yangShengEat;
    }

    @Override
    public YangShengEat updateYangShengEat(YangShengEat yangShengEat) {
        yangShengEatDao.save(yangShengEat);
        return yangShengEat;
    }

    @Override
    public Page<YangShengEat> pageYangShengEat(int pageNo, int pageSize) {
        pageNo = pageNo - 1 <= 0 ? 0 : pageNo - 1;
        Pageable pageable = PageRequest.of(pageNo, pageSize, Sort.Direction.ASC, "id");
        Page<YangShengEat> result = yangShengEatDao.findAllByStatus(pageable, STATUS_1);
        return result;
    }

    @Override
    public void deleteYangShengEat(long id) {
        YangShengEat yangShengEat = yangShengEatDao.findById(id).orElse(null);
        if (yangShengEat == null) {
            throw new EmptyException("没有此数据");
        }
        yangShengEat.setStatus(STATUS_0);
        yangShengEatDao.saveAndFlush(yangShengEat);
    }

    /**
     * 运动
     **/
    @Override
    public void saveYangeShengSports(SportsInputVo yangShengSportInput) {
        YangShengSports yangShengSports = new YangShengSports();
        BeanUtils.copyProperties(yangShengSportInput, yangShengSports);
        yangShengSportDao.save(yangShengSports);
    }

    @Override
    public YangShengSports getYangShengSportsById(long id) {
        YangShengSports yangShengSports = yangShengSportDao.findById(id).orElse(null);
        if (yangShengSports == null) {
            throw new EmptyException(" 没有相关数据 ");
        }
        return yangShengSports;
    }

    @Override
    public YangShengSports getYangShengSportsByName(String name) {
        YangShengSports yangShengSports = yangShengSportDao.findOneBySPNameAndStatus(name, STATUS_1);
        return yangShengSports;
    }

    @Override
    public YangShengSports updateYangShengSports(SportsInputVo yangShengSportInput) {
        if (yangShengSportInput.getId() > 0) {
            YangShengSports yangShengSports = new YangShengSports();
            BeanUtils.copyProperties(yangShengSportInput, yangShengSports);
            yangShengSportDao.save(yangShengSports);
            return yangShengSports;
        }else {
            throw new BusinessException("id有误");
        }

    }

    @Override
    public Page<YangShengSports> pageYangShengSports(int pageNo, int pageSize) {
        pageNo = pageNo - 1 <= 0 ? 0 : pageNo - 1;
        Pageable pageable = PageRequest.of(pageNo, pageSize, Sort.Direction.ASC, "id");
        Page<YangShengSports> result = yangShengSportDao.findAllByStatus(pageable, STATUS_1);
        return result;
    }

    @Override
    public void deleteYangShengSports(long id) {
        YangShengSports yangShengSports = yangShengSportDao.findById(id).get();
        yangShengSports.setStatus(STATUS_0);
        yangShengSportDao.saveAndFlush(yangShengSports);
    }

    /**
     * 药浴
     **/
    @Override
    public void saveYangShengMethod(YangShengMethod yangShengMethod) {
        yangShengMethodDao.save(yangShengMethod);

    }

    @Override
    public YangShengMethod getYangShengMethodById(long id) {
        YangShengMethod yangShengMethod = yangShengMethodDao.findById(id).orElse(null);
        if (yangShengMethod == null) {
            throw new EmptyException(" 没有相关数据 ");
        }
        return yangShengMethod;
    }

    @Override
    public YangShengMethod getYangShengMethodByName(String name) {
        YangShengMethod yangShengMethod = yangShengMethodDao.findOneByMedicineNameAndStatus(name, STATUS_1);
        return yangShengMethod;
    }

    @Override
    public YangShengMethod updateYangShengMethod(YangShengMethod yangShengMethod) {
        yangShengMethodDao.save(yangShengMethod);
        return yangShengMethod;
    }

    @Override
    public Page<YangShengMethod> pageYangShengMethod(int pageNo, int pageSize) {
        pageNo = pageNo - 1 <= 0 ? 0 : pageNo - 1;
        Pageable pageable = PageRequest.of(pageNo, pageSize, Sort.Direction.ASC, "id");
        Page<YangShengMethod> result = yangShengMethodDao.findAllByStatus(pageable, STATUS_1);
        return result;
    }

    @Override
    public void deleteYangShengMethod(long id) {
        YangShengMethod yangShengMethod = yangShengMethodDao.findById(id).get();
        yangShengMethod.setStatus(STATUS_0);
        yangShengMethodDao.saveAndFlush(yangShengMethod);
    }

    /**
     * 季节
     **/
    @Override
    public void saveYangShengSeason(YangShengSeaon yangShengSeaon) {
        yangShengSeaonDao.save(yangShengSeaon);
    }

    @Override
    public YangShengSeaon getYangShengSeaonById(long id) {
        YangShengSeaon yangShengSeaon = yangShengSeaonDao.findById(id).orElse(null);
        if (yangShengSeaon == null) {
            throw new EmptyException(" 没有相关数据 ");
        }
        return yangShengSeaon;
    }

    @Override
    public YangShengSeaon updateYangShengSeaon(YangShengSeaon yangShengSeaon) {
        yangShengSeaonDao.save(yangShengSeaon);
        return yangShengSeaon;
    }

    @Override
    public Page<YangShengSeaon> pageYangShengSeaon(int pageNo, int pageSize) {
        pageNo = pageNo - 1 <= 0 ? 0 : pageNo - 1;
        Pageable pageable = PageRequest.of(pageNo, pageSize, Sort.Direction.ASC, "id");
        Page<YangShengSeaon> result = yangShengSeaonDao.findAllByStatus(pageable, STATUS_1);
        return result;
    }

    @Override
    public void deleteYangShengSeaon(long id) {
        YangShengSeaon yangShengSeaon = yangShengSeaonDao.findById(id).get();
        yangShengSeaon.setStatus(STATUS_0);
        yangShengSeaonDao.saveAndFlush(yangShengSeaon);
    }

    /**
     * 楔子
     **/
    @Override
    public void saveYangShengXieZi(YangShengXieZi yangShengXieZi) {
        yangShengXieZiDao.save(yangShengXieZi);
    }

    @Override
    public YangShengXieZi getYangShengXieZiById(long id) {
        YangShengXieZi yangShengXieZi = yangShengXieZiDao.findById(id).orElse(null);
        if (yangShengXieZi == null) {
            throw new EmptyException(" 没有相关数据 ");
        }
        return yangShengXieZi;
    }

    @Override
    public YangShengXieZi updateYangShengXieZi(YangShengXieZi yangShengXieZi) {
        yangShengXieZiDao.save(yangShengXieZi);
        return yangShengXieZi;
    }

    @Override
    public Page<YangShengXieZi> pageYangShengXieZi(int pageNo, int pageSize) {
        pageNo = pageNo - 1 <= 0 ? 0 : pageNo - 1;
        Pageable pageable = PageRequest.of(pageNo, pageSize, Sort.Direction.ASC, "id");
        Page<YangShengXieZi> result = yangShengXieZiDao.findAllByStatus(pageable, STATUS_1);
        return result;
    }

    @Override
    public void deleteYangShengXieZi(long id) {
        YangShengXieZi yangShengXieZi = yangShengXieZiDao.findById(id).get();
        yangShengXieZi.setStatus(STATUS_0);
        yangShengXieZiDao.saveAndFlush(yangShengXieZi);
    }

}
